const express = require("express");
const FilmModel = require("../models/film.js");

const router = express.Router();

// 注册路由
router.get("/films", async (req, res) => {
  try {
    // 取入参
    const { page = 1, pageSize = 10 } = req.query;

    const docs = await FilmModel.find({}, "filmId name", {
      skip: (page - 1) * pageSize,
      limit: pageSize,
    });
    // 获取总条数
    const total = await FilmModel.count();

    res.send({
      code: 0,
      msg: "ok",
      data: {
        pageTotal: Math.ceil(total / pageSize), // 总页数
        total: total, // 总条数
        list: docs,
      },
    });
  } catch (error) {
    res.send({
      code: -1,
      msg: error.message || error,
      data: null,
    });
  }
});

module.exports = router;
